import sqlite3, os

class DBEngine:
    def setCipher(self,cipher):
        self.cipher = cipher
    def connect(self):
        if not self.cipher:
            return False
        if os.path.exists("%s/.dropsecure/files.db.enc" %os.getcwd()):
            self.__open()
        self.conn = sqlite3.connect("%s/.dropsecure/files.db" %os.getcwd())
        self.conn.isolation_level = None
        curse = self.conn.cursor()
        curse.execute("create table if not exists files(filename,hash,IV)")
        self.conn.commit()
        curse.close()
    def disconnect(self):
        self.conn.close()
        self.__close()
    def __open(self):
        ob = ''
        with open("%s/.dropsecure/files.db.enc" %os.getcwd(), 'rb') as f:
            ob = f.read()
        data = self.cipher.decrypt(ob, IV=13766)
        with open("%s/.dropsecure/files.db" %os.getcwd(), 'wb') as f:
            f.write(data)
        os.remove("%s/.dropsecure/files.db.enc" %os.getcwd())   
    def __close(self):
        ob = ''
        with open("%s/.dropsecure/files.db" %os.getcwd(), 'rb') as f:
            ob = f.read()
        IV, data = self.cipher.encrypt(ob, IV=13766)
        with open("%s/.dropsecure/files.db.enc" %os.getcwd(), 'wb') as f:
            f.write(data)
        os.remove("%s/.dropsecure/files.db" %os.getcwd())
        
    def insert(self,data):
        curse = self.conn.cursor()
        curse.executemany("insert into files (filename,hash,IV) values (?,?,?)",[(data['filename'],data['hash'],data['IV'])])
        self.conn.commit()
        curse.close()
        
    def updateHash(self,data):
        curse = self.conn.cursor()
        curse.execute("update files set hash = ? where filename = ?", (data['hash'],data['filename']))
        self.conn.commit()
        curse.close()
    
    def updateName(self,data):
        curse = self.conn.cursor()
        curse.execute("update files set filename = ? where hash = ?", (data['filename'],data['hash']))
        self.conn.commit()
        curse.close()
    
    def updateIV(self,data):
        result = True
        curse = self.conn.cursor()
        curse.execute("update or replace files set IV=(?) where filename=(?)", (data['IV'],data['filename']))
        self.conn.commit()
        if curse.rowcount is not 1:
            result = False
        curse.close()
        return result
        
    def getHash(self, filename):
        curse = self.conn.cursor()
        curse.execute("select hash from files where filename = ?", (filename,))
        data = curse.fetchone()
        curse.close()
        if data is None:
            data = False
        else:
            data = data[0]
        return data
    
    def getIV(self,filename):
        curse = self.conn.cursor()
        curse.execute("select IV from files where filename = ?", (filename,))
        data = curse.fetchone()
        curse.close()
        if data is None:
            data = False
        else:
            data = data[0]
        return data

    def getFiles(self):
        curse = self.conn.cursor()
        curse.execute("select filename from files")
        data = curse.fetchall()
        curse.close()
        return data
    
    def remove(self,filename):
        curse = self.conn.cursor()
        curse.execute("delete from files where filename = ?", (filename,))
        self.conn.commit()
        curse.close()